

//加载方法
$(function (){
    $.ajax({
        url: "/DHMManufacture/queryByCheckTag0",
        type:"get",
        success:function (data){
            $("#zongshu").html(data);
        }
    })
//模糊查询的方法，生成表格
    $("#shTable").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/DHMManufacture/queryAllByCheckTag",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:5,//页大小
        pageList:[3,5,9],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
            }
        },
        columns:[
            {title:'生产派工单编号',field:'manufactureId',align:'center'},
            {title:'产品编号',field:'productId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'数量',field:'amount',align:'center'},
            {title:'生产计划序号组',field:'applyIdGroup',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'审核',field:'id',align:'center',
                formatter:function (value,row,index) {
                    let btn="<button class='btn btn-info' onclick='shenhe("+value+",this)'>去审核</button>";
                    return btn;
                }
            },
        ]
    })
})
let iidd=0;
let moduleAmount=0;
let moduleTotal=0;
let reasonexactP="";
let sPayDetails=[];
//点击去审核
function shenhe(id,that){
    //进入单页
    iidd=id;
    $(".div1").hide();
    $(".div2").show();
    document.getElementById("mManufactureForm").reset();
    reasonexactP = $(that).parent().parent().find("td:eq(0)").html();
    //查询产品数量和信息
    $.ajax({
        url:"/DHMManufacture/byId/"+id,
        type:"get",
        dataType:"json",
        success:function (date){
            $("input[name='designer']").val(parent.document.getElementsByClassName("user_name")[0].innerHTML);
            $("input[name='register']").val(date.register);
            $("input[name='productId']").val(date.productId);
            $("input[name='applyIdGroup']").val(date.applyIdGroup);
            $("input[name='productName']").val(date.productName);
            $("textarea[name='productDescribe']").val(date.productDescribe);
            $("input[name='amount']").val(date.amount);
            $("textarea[name='remark']").val(date.remark);

            let data = date.mProcedures;
            let tr="";
            let moduleArr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].procedureName+"</td>\n" +
                    "                    <td>"+data[i].procedureId+"</td>\n" +
                    "                    <td>"+data[i].procedureMark+"</td>\n" +
                    "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                    <td><a href='#' onclick='watchModule("+data[i].id+")'" +
                    " data-toggle=\"modal\" data-target=\"#myModal\">查看物料</a></td>\n" +
                    "                </tr>";
                if (i==0){
                    moduleArr+=data[i].id;
                }else{
                    moduleArr+=","+data[i].id;
                }

            }
            $("#tbodyForTr").html(tr);
            $.ajax({
                url:"/DHMManufacture/queryModuleAmount",
                type:"get",
                data:{
                    arr:moduleArr
                },
                success:function (data){
                    moduleAmount=data;
                }
            })
            $.ajax({
                url:"/DHMManufacture/queryModuleSubtotal",
                type:"get",
                data:{
                    arr:moduleArr
                },
                success:function (data){
                    moduleTotal=data;
                }
            })
            $.ajax({
                url:"/DHMManufacture/queryArrModuleDetails",
                type:"get",
                data:{
                    arr:moduleArr
                },
                dataType:"json",
                success:function (data){
                    sPayDetails=data;
                }
            })
        }
    })

}


//查看物料
function watchModule(moduleParentId){

    $.ajax({
        url:"/DHMManufacture/queryByParentIdPM",
        type:"get",
        data:{
            parentId:moduleParentId
        },
        dataType:"json",
        success:function (data){
            let tr="";

            for (let i = 0; i < data.length; i++) {
                let unit = data[i].amountUnit;
                if (unit==undefined){
                    unit="";
                }
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].productName+"</td>\n" +
                    "                    <td>"+data[i].productId+"</td>\n" +
                    "                    <td></td>\n" +
                    "                    <td>"+data[i].amount+"</td>\n" +
                    "                    <td>"+unit+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                </tr>";
            }
            $("#moduleWatch").html(tr);
        }
    })
}


//返回列表
function returnList(){
    $(".div1").show();
    $(".div2").hide();
}

//通过
function adopt(){
    let myDate = new Date();

    let year=myDate.getFullYear();        //获取当前年
    let month=myDate.getMonth()+1;   //获取当前月
    let date=myDate.getDate();            //获取当前日


    let h=myDate.getHours();              //获取当前小时数(0-23)
    let m=myDate.getMinutes();          //获取当前分钟数(0-59)
    let s=myDate.getSeconds();

    let now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);

    if (confirm("确定要提交审核吗？")){
        let aManufacture={
            id:iidd,
            checker:$("input[name='designer']").val(),
            checkTag:1,
            checkTime:now,
        }
        $.ajax({
            url:"/DHMManufacture/up",
            type: "put",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(aManufacture),
            success:function (data){
                $(".div1").show();
                $(".div2").hide();
                $("#shTable").bootstrapTable("refresh");
            }
        })

        let sPay={
            id:$("input[name='amount']").val(),
            reasonexact: "派工单号："+reasonexactP,
            amountSum: moduleAmount*$("input[name='amount']").val(),
            paidAmountSum:moduleAmount*$("input[name='amount']").val(),
            costPriceSum: moduleTotal*$("input[name='amount']").val(),
            storer:parent.document.getElementsByClassName("user_name")[0].innerHTML,
            register:parent.document.getElementsByClassName("user_name")[0].innerHTML,
            storeTag:4,
            checkTag:1,
            sPayDetails:sPayDetails,
        };
        $.ajax({
            url:"/DHMManufacture/insertSPay",
            type:"post",
            data:JSON.stringify(sPay),
            contentType:"application/json;charset=utf-8",
            success:function (data){}
        })
    }
}

//不通过
function noAdopt(){
    let myDate = new Date();

    let year=myDate.getFullYear();        //获取当前年
    let month=myDate.getMonth()+1;   //获取当前月
    let date=myDate.getDate();            //获取当前日


    let h=myDate.getHours();              //获取当前小时数(0-23)
    let m=myDate.getMinutes();          //获取当前分钟数(0-59)
    let s=myDate.getSeconds();

    let now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);

    if (confirm("确定要提交审核吗？")){
        let aManufacture={
            id:iidd,
            checker:$("input[name='designer']").val(),
            checkTag:2,
            checkTime:now,
        }
        $.ajax({
            url:"/DHMManufacture/up",
            type: "put",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(aManufacture),
            success:function (data){
                $(".div1").show();
                $(".div2").hide();
                $("#shTable").bootstrapTable("refresh");
            }
        })
    }
}

//判断是否在前面加0
function getNow(s) {
    return s < 10 ? '0' + s: s;
}